package com.jb.repair.fragment;


import android.annotation.TargetApi;
import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.jb.repair.R;
import com.jb.repair.acticity.CheckLocActivity;
import com.jb.repair.adapter.CheckRegisParentAdapter;
import com.jb.repair.adapter.HisTaskAdapter;
import com.jb.repair.adapter.TodayTaskAdapter;
import com.jb.repair.dao.QueryDb;
import com.jb.repair.hardware.nfc.TagViewerActivity;
import com.jb.repair.model.TaskModel;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by gaobin on 2015/10/23.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainFragment extends Fragment implements AdapterView.OnItemClickListener {
    private TextView tv_today_date, tv_update_date;
    private ListView lv_today_task, lv_his_task;
    private ToggleButton tb;

    private SharedPreferences sp1;
    private List<TaskModel> todayTaskList;
    private List<TaskModel> hisTaskList;
    private TodayTaskAdapter todayTaskAdapter;
    private HisTaskAdapter hisTaskAdapter;
    private Boolean isB = false;

    private final String TAG = "MainFragment";

    public MainFragment() {
    }

    @Override
    public void onResume() {
        super.onResume();
        todayTaskList = QueryDb.getInstance(getActivity()).getTodayTask(isB);
        todayTaskAdapter = new TodayTaskAdapter(getActivity(), todayTaskList);
        if(todayTaskList.size() > 0){
            CheckLocActivity.task_id = todayTaskList.get(0).guid;
        }
        lv_today_task.setAdapter(todayTaskAdapter);
        hisTaskList = QueryDb.getInstance(getActivity()).getHisTask(isB);
        hisTaskAdapter.notifyDataSetChanged();
        tb.setChecked(isB);

        // 修改页面状态，禁止再次扫码
        TagViewerActivity.NOW_IN_PROPER_PAGE = false;
    }

    @Override
    public void onStop() {
        super.onStop();
        todayTaskList = null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.gc();
        sp1 = getActivity().getSharedPreferences("changable.txt", getActivity().MODE_PRIVATE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frament_main, null);
        initView(view);
        hisTaskList = QueryDb.getInstance(getActivity()).getHisTask(isB);
        hisTaskAdapter = new HisTaskAdapter(getActivity(), hisTaskList);
        lv_his_task.setAdapter(hisTaskAdapter);
        return view;
    }

    private void initView(View view) {
        tv_today_date = (TextView) view.findViewById(R.id.tv_today_date);
        tv_update_date = (TextView) view.findViewById(R.id.tv_update_date);
        tb = (ToggleButton) view.findViewById(R.id.tb);

        tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isB = isChecked;
                todayTaskList = QueryDb.getInstance(getActivity()).getTodayTask(isB);
                todayTaskAdapter = new TodayTaskAdapter(getActivity(), todayTaskList);
                hisTaskList = QueryDb.getInstance(getActivity()).getHisTask(isB);
                hisTaskAdapter = new HisTaskAdapter(getActivity(),hisTaskList);
                lv_his_task.setAdapter(hisTaskAdapter);
                lv_today_task.setAdapter(todayTaskAdapter);
            }
        });

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
        String s = formatter.format(new Date());

        SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy.MM.dd E");
        String s2 = formatter2.format(new Date());
        tv_today_date.setText(s2);

        lv_today_task = (ListView) view.findViewById(R.id.lv_today_task);
        lv_his_task = (ListView) view.findViewById(R.id.lv_his_task);
        lv_today_task.setOnItemClickListener(this);
        lv_his_task.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position > 0) {
            Intent intent = new Intent(getActivity(), CheckLocActivity.class);
            if (parent.getId() == R.id.lv_today_task) {
                CheckLocActivity.task_id = todayTaskList.get(position - 1).guid;
                sp1.edit().putBoolean("changable", true).commit();
                CheckRegisParentAdapter.IS_HISTORY = false; // 跳转到今天任务列表
            } else if (parent.getId() == R.id.lv_his_task) {
                CheckLocActivity.task_id = hisTaskList.get(position - 1).guid;
                sp1.edit().putBoolean("changable", false).commit();
                CheckRegisParentAdapter.IS_HISTORY = true; // 跳转到历史任务列表
            }
            startActivity(intent);
        }
    }

}
